<?php
/**
 * form控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_form extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadClass('Mcache');
		$this->loadModel('form_model');
		$this->loadModel('response_model');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/form/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{
		$this->getForms();
	}

	/**
	 * 保存表单
	 * @return boolean
	 */
	function saveForm(){

		$data     = json_decode($this->in_var[data]);

		$formKey  = trim($this->in_var['formKey']);
		$tpl_name = "preview_common.html";
		
		//清空缓存
		$this->cleanCachePage($formKey);
		
		echo $this->form_model->saveForm($formKey,$data);
	}

	/**
	 * 清空文件缓存
	 * @return unknown_type
	 */
	private function cleanCachePage($formKey){
		
		$tpl_name_common   = "preview_common.html";
		$tpl_name_page     = "preview_page.html";
		$preview_feedback  = "preview_feedback.html";
		
		//后期会针对目录进行清理
		$this->tpl->template_dir = SYS_PATH  . 'template/web/form/';

		$this->tpl->clear_cache_dir($tpl_name_common,$formKey);
	
		$this->tpl->clear_cache_dir($tpl_name_page,$formKey);
	
		$this->tpl->clear_cache_dir($preview_feedback,$formKey);
		
	}

	/**
	 * 获取表单列表
	 * @return response
	 */
	function getForms(){

		$page = max(1,intval($this->in_var['page']));

		$list = $this->form_model->getFormItemsByKeywords('',$page,5);

		$page_info = $this->form_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->result['list'] = $list;

		$this->tpl->assign("result",$this->result);
		$this->tpl->assign("page_info",$page_info);

		$this->tpl->display('formlist.html');
	}

	/**
	 * 表单编辑
	 * @return response
	 */
	function gform(){

		$key  = $this->in_var['key'];
		$form = $this->form_model->getFormItemByKey($key);

		if(empty($form))
		{
			$fk = md5(time());
			$fm = array('f_title'=>'form title','f_key'=>$fk,'f_start_time'=>time());
			
			$is_success = $this->form_model->addFormItem($fm);
			
			if($is_success)
			{
				$new_form = $this->form_model->getFormByKey($fk);
				
				$this->result['form'] = json_encode($new_form);
			}
			
			Goto("/admin/admin_form/gform?key=".$fk,'header','document',false);
		}
		else
		{
			$this->result['form'] = json_encode($this->form_model->getFormByKey($key));
			
			$this->tpl->assign("result",$this->result);

			$this->tpl->display("gform.html");
		}
	}

	/**
	 * 获取引用脚本
	 * @return response
	 */
	function getScript(){

		$key    = $this->in_var['key'];
		$host   = $_SERVER['HTTP_HOST'];
		$type   = $this->in_var["type"];
		$modal  = $this->in_var["modal"];
		$form   = $this->form_model->getFormItemByKey($key);
		
		$this->result['host'] = $host;
		$this->result['key']    = $key;
		$this->result['type']   = $type;
		$this->result['modal']  = $modal;
		$this->result['height'] = intval($form['f_height']);
		$this->result['width']  = intval($form['f_width']);

		$this->tpl->assign('result',$this->result);
		
		$this->tpl->display("script.html");
	}
	
	/**
	 * 获取脚本面板
	 * @return max
	 */
	function scriptPanel(){
		
		$tpl_name = "scriptpanel.html";
		
		$this->result['key'] = $this->in_var['key'];
		
		$this->tpl->assign('result',$this->result);
		
		$this->tpl->display($tpl_name);
	}

	/**
	 * 批量删除表单
	 * @return json
	 */
	function delForms(){

		$ids = $this->in_var['item'];

		$id_ = explode(',',$ids);

		try{
			foreach($id_ as $id)
			{
				$this->form_model->delFormItemById($id);
			}
		}
		catch(Exception $e)
		{
			exit(json_encode(array("info"=>$this->sys_conf_var['survey']['customer_msg']['form']['del_error'],"status"=>0)));
		}

		exit(json_encode(array("info"=>$this->sys_conf_var['survey']['customer_msg']['form']['del_success'],"status"=>1)));

	}

	/**
	 * 删除单个表单
	 * @return json
	 */
	function delForm(){

		$key = $this->in_var['item'];

		$result = $this->form_model->delFormItemByKey($key,true);

		if($result)
		{
			echo json_encode(array("info"=>$this->sys_conf_var['survey']['customer_msg']['form']['del_success'],"status"=>1));
		}
		else
		{
			echo json_encode(array("info"=>$this->sys_conf_var['survey']['customer_msg']['form']['del_error'],"status"=>0));
		}
	}
	
	/**
	 * 获取预览控制面板
	 * @return unknown_type
	 */
	function previewPanel(){
		
		$tpl_name = "previewpanel.html";
		
		$this->result['key'] = $this->in_var['key'];
		
		$this->tpl->assign('result',$this->result);
		$this->tpl->display($tpl_name);
	}
	
	/**
	 * 清空面板操作
	 * @return unknown_type
	 */
	function clearPanel(){
		
		$tpl_name = "clearpanel.html";
		
		$this->result['key'] = $this->in_var['key'];
		
		$this->tpl->assign('result',$this->result);
		$this->tpl->display($tpl_name);
	}
	
	/**
	 * 清空缓存
	 * @return unknown_type
	 */
	function clearCache(){
		
		$type = $this->in_var["type"];
		$key  = $this->in_var["key"];
		
		switch($type){
			case 'file':
				$this->cleanCachePage($key);
				break;
			case 'data':
				$this->cleardata($key);
				break;
			default:
				break;
		}
		
		//执行完毕就成功
		echo 1;
		
	}
	
	/**
	 * 清空统计数据
	 * @return max
	 */
	private function cleardata($key){
		
		$this->Mcache->clearCache("survey",$key);
		
		$this->loadModel('response_model');
		
		echo $this->response_model->clearData($key);
	}
	

}